package com.janus.multitypedemo.vb;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.janus.multitypedemo.R;
import com.janus.multitypedemo.bean.HotList;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by janus on 2017/7/10.
 * BannerViewBinder
 */

public class HotViewBinder extends ItemViewBinder<HotList, HotViewBinder.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.layout_hot, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final HotList item) {
        Glide.with(holder.linearLayout.getContext())
                .load(item.bg_url)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            Drawable drawable = new BitmapDrawable(resource);
                            holder.linearLayout.setBackground(drawable);
                        }
                    }
                });
        List<TextView> titles = new ArrayList<>();
        titles.add(holder.tvTitle1);
        titles.add(holder.tvTitle2);
        titles.add(holder.tvTitle3);
        List<TextView> ts = new ArrayList<>();
        ts.add(holder.tvTS1);
        ts.add(holder.tvTS2);
        ts.add(holder.tvTS3);
        List<ImageView> ivs = new ArrayList<>();
        ivs.add(holder.iv1);
        ivs.add(holder.iv2);
        ivs.add(holder.iv3);
        for (int i = 0; i < item.hots.size(); i++) {
            titles.get(i).setText(item.hots.get(i).title);
            ts.get(i).setText(item.hots.get(i).type_size);
            Glide.with(ivs.get(i).getContext())
                    .load(item.hots.get(i).url)
                    .into(ivs.get(i));
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout linearLayout;
        TextView tvTitle1, tvTitle2, tvTitle3;
        TextView tvTS1, tvTS2, tvTS3;
        ImageView iv1, iv2, iv3;

        public ViewHolder(View itemView) {
            super(itemView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            tvTitle1 = (TextView) itemView.findViewById(R.id.tv_hot_title_1);
            tvTitle2 = (TextView) itemView.findViewById(R.id.tv_hot_title_2);
            tvTitle3 = (TextView) itemView.findViewById(R.id.tv_hot_title_3);
            tvTS1 = (TextView) itemView.findViewById(R.id.tv_hot_type_size_1);
            tvTS2 = (TextView) itemView.findViewById(R.id.tv_hot_type_size_2);
            tvTS3 = (TextView) itemView.findViewById(R.id.tv_hot_type_size_3);
            iv1 = (ImageView) itemView.findViewById(R.id.iv_hot_1);
            iv2 = (ImageView) itemView.findViewById(R.id.iv_hot_2);
            iv3 = (ImageView) itemView.findViewById(R.id.iv_hot_3);
        }

    }
}
